<?php
/**
 * Members area welcome script
 * 
 * Tracks membership login into NATS4 database, checks the member's signup level
 * and redirects to apropriate membership area section
 * 
 * @name		welcome member
 * @version		1.2
 * @uses		Nats4space_Members
 * @package		nats-space
 * @see			http://code.google.com/p/nats-space/
 * @copyright	2008-2009
 * @author		Joseph C. <joseph.cheys@gmail.com>
 */

/**
 * Welcome Member - script configuration
 */

/**
 * Affects errors output and debug information output, redirect will be disabled
 */ 
define('DEBUG_MODE', false);

/**
 * Affiliate tracking system URL
 */
define('NATS_URL', 'http://nats.yourdomain.com/');

/**
 * Full membership area URL 
 */
define('FULL_URL', '/full/');

/**
 * Trial membership URL
 */
define('TRIAL_URL', '/trial/');

/**
 * NATS site ID values, if current member area server couple sites 
 * then this can be coma separated values, fo ex. 5,64,2
 */
define('SITE_IDS', '1');
 
/**
 * Library path 
 * (can be blank if this path is added into your inlude path)
 * NOTE! don't forget to add trailing slash
 */
define('LIB_DIR', ''); // set to /path/library/



/**
 * NATS config
 * You can define the path to NATS config file
 * and we will read the database accesses from that file  
 * OR... (to be continued, at the bottom  V )
 */
define('NATS_CONFIG', '/path-to-nats/nats/includes/config.php');

if (file_exists(NATS_CONFIG)) { // read database accesses
	
	require_once NATS_CONFIG; 
	define('DB_SERVER', $config['DB_SERVER']);
	define('DB_USER', $config['DB_USER']);
	define('DB_PASSWORD', $config['DB_PASSWORD']);
	define('DB_DB', $config['DB_DB']);

} else {
/**
 * ...OR you can provide the database accesses directly here
 */
	define('DB_SERVER',		'localhost');
	define('DB_USER',		'--databse user name --');
	define('DB_PASSWORD',	'--database password --');
	define('DB_DB',			'--database name--');
}








/**
 * END OF CONFIGURATION
 * 
 * WARNING!!! Do not modify the code above 
 * unleast you are realy know what you are doing.
 */

/**
 * Read required session/environment data
 */

/**
 * These values are preconfigured already 
 * so you don't need to change them
 * 
 * Possibly in next NATS varsion they will change
 * their full/trial memberhip level codes
 * but for now there is nothing to change
 * 
 * @todo: We can refer to the following constants
 * 		  Nats4space_Users_Management::MEMBERSHIP_LEVEL_TRIAL
 *        Nats4space_Users_Management::MEMBERSHIP_LEVEL_FULL
 */
define('NATS_MEMBERSHIP_LEVEL_TRIAL', 1);
define('NATS_MEMBERSHIP_LEVEL_FULL', 0);

/**
 * Asuming that this member was authenticated via mod_auth_mysql or similar
 */
define('MEMBER_LOGIN', $_SERVER['PHP_AUTH_USER']); 

/**
 * Get member IP address
 */
define('MEMBER_IP', $_SERVER['REMOTE_ADDR']);

/**
 * Init includepath
 */
$paths = explode(PATH_SEPARATOR, get_include_path());
$paths[] = LIB_DIR;
set_include_path(implode(PATH_SEPARATOR, $paths));
unset($paths);

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Nats4space_');

/**
 * Track memberhsip login
 */
$result = Nats4space_Members::TrackLogin( // This is my class and I love it ;)
	  NATS_URL
	, MEMBER_LOGIN
	, MEMBER_IP
	, SITE_IDS
);

if (DEBUG_MODE && (false === $result)) {
	die(Nats4space_Members::getLastErrorMsg());
}

/**
 * Check membership level
 * @todo Use Zend_Db to avoid problems on new (cool!) hosts
 * 		 where only mysqli or/and PDO extensions available
 */
$dbLink = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);

/**
 * Set current db
 */
$dbSelected = mysql_select_db(DB_DB, $dbLink);
if (!$dbSelected) {
    die ('Can\'t select databse: ' . DB_DB . ' - ' . mysql_error());
}

$sql = '
	SELECT
		trial 
	FROM 
		member
	WHERE 
		username = "' . MEMBER_LOGIN . '"
	ORDER BY
		memberid DESC';

$resDb = mysql_query($sql, $dbLink);

/**
 * Die with mySQL error
 * @todo: add errors processing in advance
 */
if (!$resDb) {
	die(mysql_error($dbLink));
}

$rowData = mysql_fetch_array($resDb);

/**
 * In case session is not started automatically
 */
if (!isset($_SESSION)) {
	session_start();
}

/**
 * Store value into session for possible further use
 */
$_SESSION['trial'] = $rowData['trial'];

/**
 * Redirect member
 */
if (!DEBUG_MODE) {
switch ($rowData['trial']) {
	
	case NATS_MEMBERSHIP_LEVEL_FULL:
		header('Location: ' . FULL_URL);
	break;
	
	case NATS_MEMBERSHIP_LEVEL_TRIAL:
		header('Location: ' . TRIAL_URL);
	break;
	
	default:
		header('Location: ' . TRIAL_URL);
}} else {
	echo 'Membership code: ' . $rowData['trial'];
	die();
}